package com.athuiju.fenxiaoback.controller;


import com.athuiju.fenxiaoback.entity.TbCartitem;
import com.athuiju.fenxiaoback.entity.vo.OrderConfirmVo;
import com.athuiju.fenxiaoback.service.TbCartService;
import com.athuiju.fenxiaoback.service.TbCartitemService;
import com.athuiju.fenxiaoback.utils.JwtUtils;
import com.athuiju.fenxiaoback.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.*;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author athuiju
 * @since 2021-07-02
 */
//@CrossOrigin
@Api(description = "购物车",tags = "购物车")
@RestController
@RequestMapping("/fenxiaoback/tb-cart")
public class TbCartController {
    @Autowired
    TbCartService cartService;

    @Autowired
    TbCartitemService cartitemService;




    @ApiOperation(value = "设置购物项是否选中---1:选中  0:不选中")
    @GetMapping("/selectItem/{skuid}/{isCheck}")
    public R selectItem(@ApiParam("示例：5025992") @PathVariable("skuid") String skuid, @ApiParam("示例：1   1,选中   0,不选中") @PathVariable("isCheck") Integer isCheck, HttpServletRequest request){

        String memberid = JwtUtils.getMemberIdByJwtToken(request);
        QueryWrapper<TbCartitem> itemWrapper=new QueryWrapper<>();
        itemWrapper.eq("memberid",memberid);
        itemWrapper.eq("skuId",skuid);
        TbCartitem item = cartitemService.getOne(itemWrapper);



        item.setIsCheck(isCheck==1 ? true:false);


        QueryWrapper<TbCartitem> itemWrapper01=new QueryWrapper<>();
        itemWrapper01.eq("memberid",memberid);
        itemWrapper01.eq("skuId",skuid);

        cartitemService.update(item,itemWrapper01);


        return R.ok().put("msg","修改成功");
    }



    @ApiOperation(value = "添加到购物车")
    @GetMapping("/addToCart/{skuId}/{num}")
    public R addCart(@ApiParam("示例：5025992")@PathVariable("skuId") Long skuId, @PathVariable("num") Integer num, HttpServletRequest request) throws ExecutionException, InterruptedException {

        //boolean b = JwtUtils.checkToken(request);
        ////
        //if (b){
        //
            String memberid = JwtUtils.getMemberIdByJwtToken(request);
            TbCartitem cartItem=cartService.addToCart(skuId, num,memberid);
            return R.ok();

        //}else{
        //    return R.error("请登录");
        //}


    }

    @ApiOperation(value = "查询用户购物车列表")
    @GetMapping("/selectMemberCart")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = TbCartitem.class),
    })
    public R selectMemberCart(HttpServletRequest request){

        boolean b = JwtUtils.checkToken(request);
        //
        if (b){

            String memberid = JwtUtils.getMemberIdByJwtToken(request);
            List<TbCartitem> cartItems=cartService.selectMemberCart(memberid);
            if (cartItems==null || cartItems.size()==0){
                return R.error().put("msg","此用户购物车为空").put("data",new ArrayList<TbCartitem>());
            }
            return R.ok().put("data", cartItems);

        }else{
            return R.error("请重新登录再查询");
        }
    }


    @ApiOperation(value = "删除用户购物车中的商品")
    @PostMapping("/delMemberCartItem")
    public R delMemberCartItem(@RequestBody String[] skuids,HttpServletRequest request){
        boolean b = JwtUtils.checkToken(request);
        //
        if (b){

            String memberid = JwtUtils.getMemberIdByJwtToken(request);
            Integer count=cartService.delMemberCartItem(memberid,skuids);
            if (count==skuids.length){
                return R.ok().put("msg", "删除成功");
            } else if (count>0 && count<skuids.length){
                return R.ok().put("msg", "部分删除成功");
            }
            return R.ok().put("msg", "删除失败");
        }else{
            return R.error("请重新登录");
        }
    }



}

